package com.baosight.risk.web.xtaa.code;

import com.baosight.risk.api.xtaa.code.IXtaaCodeTypeService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.common.annotation.Log;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.enums.BusinessType;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.service.xtaa.code.entity.XtaaCodeType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;

/**
 * @desc:
 * @author: CuiShiHao
 **/
@Slf4j
@RestController
@RequestMapping("xtaa/code/type")
@Api(value = "系统字典类别", description = "系统字典类别", produces = MediaType.APPLICATION_JSON)
public class XtaaCodeTypeController extends BaseController {
    @Autowired
    private IXtaaCodeTypeService xtaaCodeTypeService;

    @ApiOperation("获取自典类别列表")
    @PostMapping("/list")
    //@RequiresPermissions("xtaa:code:type:list")
    public AjaxResult list(XtaaCodeType xtaaCodeType) {
        startPage();
        return getResultPage(xtaaCodeTypeService.select(xtaaCodeType));
    }

    @ApiOperation("根据字典类型名称模糊查询")
    @GetMapping("/fuzyListByDDTypeName")
//    @RequiresPermissions("xtaa:code:type:fuzylist")
    public AjaxResult fuzyListByDDTypeName(String ddTypeName) {
        startPage();
        return getResultPage(xtaaCodeTypeService.fuzyListByDDTypeName(ddTypeName));
    }

    @ApiOperation("获取字典类别实体")
    @RequestMapping(value = "/getItem", method = RequestMethod.GET, name = "获取字典类别")
    public AjaxResult getItem(String id) {
        XtaaCodeType xtaaCodeType = xtaaCodeTypeService.get(id);
        if (xtaaCodeType == null) {
            return error("数据不存在");
        }
        return success(xtaaCodeType);
    }

    @ApiOperation("新增字典类别")
    @PostMapping("save")
    @Log(title = "字典类型新增", businessType = BusinessType.INSERT)
    @RequiresPermissions("xtaa:code:type:save")
    public AjaxResult save(@RequestBody XtaaCodeType xtaaCodeType) {
        xtaaCodeType.setStatus("10");
        if (StringUtils.isNull(xtaaCodeType.getLevelFlag())) {
            xtaaCodeType.setLevelFlag("0");
        }
        //初始化基本参数
        xtaaCodeType = (XtaaCodeType) BeanUtils.initBean(xtaaCodeType);
        log.info("字典类型添加：" + xtaaCodeType.toString());
        return toAjax(xtaaCodeTypeService.insert(xtaaCodeType));
    }

    @GetMapping("edit/{id}")
    public AjaxResult edit(@PathVariable("id") String id) {
        return success(xtaaCodeTypeService.get(id));
    }

    @ApiOperation("更新字典类别")
    @PostMapping("update")
    @Log(title = "字典类型编辑")
    public AjaxResult update(@RequestBody XtaaCodeType xtaaCodeType) {
        log.info("字典类型编辑：" + xtaaCodeType.toString());
        return toAjax(xtaaCodeTypeService.updateType(xtaaCodeType));
    }

    @PostMapping("delete/{id}")
    @Log(title = "字典类型删除", businessType = BusinessType.DELETE)
    public AjaxResult delete(@PathVariable("id") String id) {
        return toAjax(xtaaCodeTypeService.deleteType(id));
    }
}
